package com.zhaoyue.study.zk;

import org.apache.zookeeper.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 2017/2/13
 * Time: 16:27
 * To change this template use File | Settings | File Templates.
 */
public class Consumer {
    public static void main(String[] args){

        try{

            //创建会话
            ZooKeeper zk = new ZooKeeper("192.168.56.56:2181", 3000,
                    new Watcher() {
                        @Override
                        public void process(WatchedEvent event) {
                            System.out.println("已经出发了:" + event.getType() +"事件");
                            System.out.println(event.getState().toString());
                            System.out.println(event.getPath());
                        }
                    });

            // 创建一个目录节点
            //CreateMode.PERSISTENT 持久化节点
            //ZooDefs.Ids.OPEN_ACL_UNSAFE 不需要任务权限
            zk.create("/testRootPath1", "testRootData".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
                    CreateMode.EPHEMERAL);



            //读取一个节点
            System.out.println(new String(zk.getData("/testRootPath1",false,null)));
            Thread.sleep(Integer.MAX_VALUE);

        }catch (Exception e){
            e.printStackTrace();
        }



    }
}
